Server = class('Server'):include(Stateful)


function Server:initialize()
	print("initializing server")
	self.host = enet.host_create"localhost:1337"
	self:gotoState("lobby")

	self.cmd_patterns = {
		"%[email%]",
		"%[pass%]",
		"%[stat%]",
		"%[stat_stream_finished%]",
		"%[train%]",
		"%[spec%]",
		"%[skill_stream_finished%]",
		"%[say%]",
		"%[team%]",
		"%[move_up%]",
		"%[move_down%]",
		"%[move_right%]",
		"%[move_left%]"
	}
	self.stat_patterns = {
	"%[energy%]",
	"%[grit%]",
	"%[concentration%]",
	"%[might%]",
	"%[persistence%]",
	"%[coordination%]",
	"%[lithe%]",
	"%[understanding%]",
	"%[resolve%]",
	"%[kismet%]",
	"%[name%]",
	"%[gender%]"
	}
	self.guests = {}
end

function Server:parse(text)
	print("parsing " .. text)
	local results = {}
	for i = 1, #self.cmd_patterns do
		print("testing cmd pattern " .. self.cmd_patterns[i])
		local s, e = text:find(self.cmd_patterns[i])
		if s and e then
			print("command found starting at char " .. s .. " ending at char " .. e)
			local iter = #results+1
			results[iter] = {}
			results[iter].cmd = text:sub(s, e)
			results[iter].data = text:sub(e+1)
		end
	end
	if #results > 0 then return results else return nil end
end

function Server:parse_stat(text)
	local results = {}
	for i = 1, #self.stat_patterns do
		local s, e = text:find(self.stat_patterns[i])
		if s and e then
			local iter = #results+1
			results[iter] = {}
			results[iter].cmd = text:sub(s, e)
			results[iter].data = text:sub(e+1)
		end
	end
	if #results > 0 then return results else return nil end
end

function Server:find_guest_file(email)
	local exists = love.filesystem.exists(email)
	if exists then
		local chunk = love.filesystem.load(email)
		local guest = chunk()
		print(guest.ip, guest.email, guest.pass)
		return guest
	end
	return nil
end

function Server:find_guest(ip)
	print("searching guests for ip .." .. ip)
	for i = 1, #self.guests do
		if self.guests[i] then
			print(self.guests[i].ip)
			if tostring(self.guests[i].ip) == tostring(ip) then
				print("guest found")
				return self.guests[i], i
			end
		end
	end
end


function Server:add_new_guest(ip)
	local iter = #self.guests+1
	self.guests[iter] = Guest:new(ip)
	return self.guests[iter]
end

function Server:add_guest(guest)
	local iter = #self.guests+1
	self.guests[iter] = guest
end

function Server:disconnect_guest(guest_id)
	print("disconnecting " .. self.guests[guest_id].ip)
	self.guests[guest_id] = nil
end

function Server:guest_say(guest, text)
	local s = guest.name .. " says, \"" .. text .. "\""
	print(s) 
	self.host:broadcast("[chat]"..s)
end


function Server:draw()

end

function Server:update(dt)

end

function Server:keypressed(key, isRepeat)

end

function Server:keyreleased(key)

end

function Server:mousepressed(x, y, button)

end

function Server:mousereleased(x, y, button)

end

function Server:quit()

end

require'src.server_states.lobby'
